import msg from "../../../../../../utils/msg";
import { ChangeSrInfo } from "../../../../../../utils/request/api";
import { getInterfaceRoute } from "../../../../../../utils/request/route";
import User from "../../../../../../utils/user";
var user = new User;
// pages/user/s_worker/chooseTime/s_work/detail/detail.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:{} as any,
    form:{
      type:'正常签到' as string,
      reason:'' as string,
    },
    sign_type:[] as string[],
    pickSignType:false,
    fileList: [] as any,//上传图片所用
  },
  //上传图片
  afterRead(event: any) {
    const { file } = event.detail;
    const { fileList = [] } = this.data.fileList;
    fileList.push({ ...file, url: file.url });
    this.setData({
      fileList: fileList,
    });
  },
  onClickLeft(){
    wx.navigateBack();
  },
  chooseSignType(){
    this.setData({
      pickSignType:true,
    })
  },
  inputReason(e:any){
    this.setData({
      ['form.reason']:e.detail.value,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options:any) {
    let data = JSON.parse(options.data);
    this.setData({
      data:data,
      sign_type:JSON.parse(options.sign_type),
    })
  },
  submit:async function(){
    let origin = this.data.data;
    let form = this.data.form;
    if(form.type==''){
      msg.show('修改情况不能为空！','');
      return;
    }
    if(form.reason==''){
      msg.show('修改理由不能为空！','');
      return;
    }
    if(origin.type==form.type){
      msg.show('无效修改','请选一个与原本不同的签到情况');
      return;
    }
    if(this.data.fileList.length==0){
      msg.show('修改凭证不能为空','请上传令人信服的凭证');
      return;
    }
    await msg.confirm('是否修改'+origin.s_name+'同学的签到情况从'+origin.type+' => '+form.type,'请确认');
    msg.Loading('上传中');
    let self=this;
    await ChangeSrInfo({sr_id:origin.sr_id,change:form.type,reason:form.reason,openid:user.openid})
    .then((res:any)=>{
      if(res.data.code=='200'){
        //上传凭证
        msg.Loading('上传凭证中');
        wx.uploadFile({
          url: getInterfaceRoute('ChangeSrPic'), 
          filePath: this.data.fileList[0].url,
          name: 'file',
          formData: { sr_change_id: res.data.data },
          success(res: any) {
            msg.hideLoading();
            let data = self.data.data;
            data.type=self.data.form.type;
            self.setData({
              fileList: [],
              data:data,
              form:{reason:'',type:'正常签到'},
            });
            msg.show('更改成功','刷新后显示');
            wx.navigateBack();
            if (res.data.code == '200') {
              return;
            };
          },
          fail(err) {
            msg.hideLoading();
            msg.show('凭证上传失败', '请稍后再试');
          }
        });
      }
      else msg.show('更改失败','');
    });
    msg.hideLoading();
    let data = this.data.data;
    data.type=form.type;
    this.setData({
      detailShow:false,
      data:data,
    });
    //this.cal();
  },
  onChangeSignType(e:any){
    this.setData({
      ['form.type']:e.detail.value,
    });
  },
  onClose2(){
    this.setData({
      pickSignType:false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})